package com.tinyseed.emcp.api.basicdata.repository;

import com.tinyseed.emcp.api.basicdata.domain.Organize;
import com.tinyseed.emcp.api.dashboard.query.TreeQuickQuery;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface OrganizeRepository extends  JpaRepository<Organize, Long>, JpaSpecificationExecutor<Organize> {

    @Query(value = "select id,organize_name ,parent_id from tb_organize", nativeQuery = true)
    List<Map<String, Object>> findOrganizes();

    List<Organize> findAllByParentId(Long parentId);

    @Query(value = "select new com.tinyseed.emcp.api.dashboard.query.TreeQuickQuery(o.id,o.organizeName ,o.parentId) from Organize o")
    List<TreeQuickQuery> queryOrganizeTree();

    Optional<Organize> findById(Long id);

    Optional<Organize> findByOrganizeName(String organizeName);

    @Transactional
    void deleteByParentId(Long parentId);
}
